package com.sx.pipeline.ui.map.tools

import android.graphics.Point
import com.amap.api.maps.AMap
import com.amap.api.maps.model.LatLng
import com.sx.pipeline.ui.map.entity.DrawLatLng
import kotlin.math.pow
import kotlin.math.sqrt

/**
 * @Author: YY
 * @Date: 2022/4/18
 * @Description: TODO
 */
object DrawMapUtils {

    /**
     * 获取屏幕距离
     */
    fun getScreenDistance(aMap: AMap, latLng1: LatLng?, latLng2: LatLng?): Float {
        val point1 = aMap.projection.toScreenLocation(latLng1)
        val point2 = aMap.projection.toScreenLocation(latLng2)
        return sqrt((point1.x - point2.x).toDouble().pow(2.0)
                + (point1.y - point2.y).toDouble().pow(2.0)).toFloat()
    }

    fun drawLatLngToLatLng(drawLatLngList: List<DrawLatLng>): ArrayList<LatLng> {
        val latLngList = ArrayList<LatLng>()
        for (i in drawLatLngList.indices) {
            latLngList.add(drawLatLngList[i].latLng)
        }
        return latLngList
    }

    /**
     * 检查绘制是否重叠
     */
    fun checkDrawOverlap(latLngList: MutableList<DrawLatLng>, aMap: AMap): Boolean {
        var flag = false
        try {
            var point3: Point
            var point4: Point
            for (i in 0 until latLngList.size - 2) {
                val point1 = aMap.projection.toScreenLocation(latLngList[i].latLng)
                val point2 = aMap.projection.toScreenLocation(latLngList[i + 1].latLng)

                for (j in i + 2 until latLngList.size) {
                    if (i == 0 && j == latLngList.size - 1) {
                        break
                    } else if (j == latLngList.size - 1) {
                        point3 = aMap.projection.toScreenLocation(latLngList[j].latLng)
                        point4 = aMap.projection.toScreenLocation(latLngList[0].latLng)
                        flag = intersect(point1, point2, point3, point4)
                    } else {
                        point3 = aMap.projection.toScreenLocation(latLngList.get(j).latLng)
                        point4 = aMap.projection.toScreenLocation(latLngList.get(j + 1).latLng)
                        flag = intersect(point1, point2, point3, point4)
                    }
                    if (flag) {
                        return flag
                    }
                }
            }
        } catch (e:Exception) {
            e.printStackTrace()
        }
        return flag
    }


    private fun intersect(point1: Point, point2: Point, point3: Point, point4: Point): Boolean {
        val l1x1 = point1.x.toDouble()
        val l1y1 = point1.y.toDouble()
        val l1x2 = point2.x.toDouble()
        val l1y2 = point2.y.toDouble()
        val l2x1 = point3.x.toDouble()
        val l2y1 = point3.y.toDouble()
        val l2x2 = point4.x.toDouble()
        val l2y2 = point4.y.toDouble()

        // 快速排斥实验 首先判断两条线段在 x 以及 y 坐标的投影是否有重合。 有一个为真，则代表两线段必不可交。
        if (l1x1.coerceAtLeast(l1x2) < l2x1.coerceAtMost(l2x2) || l1y1.coerceAtLeast(l1y2) < l2y1.coerceAtMost(l2y2)
            || l2x1.coerceAtLeast(l2x2) < l1x1.coerceAtMost(l1x2) || l2y1.coerceAtLeast(l2y2) < l1y1.coerceAtMost(l1y2)
        ) {
            return false
        }
        // 跨立实验  如果相交则矢量叉积异号或为零，大于零则不相交
        return !((((l1x1 - l2x1) * (l2y2 - l2y1) - (l1y1 - l2y1) * (l2x2 - l2x1))
                * ((l1x2 - l2x1) * (l2y2 - l2y1) - (l1y2 - l2y1) * (l2x2 - l2x1))) > 0
                || (((l2x1 - l1x1) * (l1y2 - l1y1) - (l2y1 - l1y1) * (l1x2 - l1x1))
                * ((l2x2 - l1x1) * (l1y2 - l1y1) - (l2y2 - l1y1) * (l1x2 - l1x1))) > 0)
    }

}